package com.project.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.project.domain.bo.AlterationBo;
import com.project.domain.entity.BizProjectAlteration;
import com.project.domain.vo.NavigationForProVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface AlterationMapper extends BaseMapper<BizProjectAlteration> {

    /**
     * 列表
     * @param page
     * @param queryWrapper
     * @param alterationBo
     * @return
     */
    @Select({
            "<script>",
            "select zb.*,zb.PROJECT_NAME as proName,Date(zb.ALTER_DATE) as alterDate,",
            "Date(CHANGE_PLAN_END_DATE) as changePlanEndDate,zb.CHECKSTATUS as checkstatus,zb.ALTER_TYPE as alterType",
            ",pb.CHARGER_NAME as chargerName,pb.CHARGER_CODE as chargerCode,bu.NAME as unitName,pb.UNIT_ID as unitId ",
            " from biz_project_alteration zb ",
            "left join v_project pb on pb.ID = zb.PROJECT_ID ",
            "left join biz_unit bu on pb.unit_id = bu.id ",
            "<if test='ew.customSqlSegment != null and ew.customSqlSegment != \"\"'>",
            "${ew.customSqlSegment} ",
            "</if>",
            "<if test='ew.customSqlSegment == null or ew.customSqlSegment == \"\"'>",
            "where 1 = 1 ",
            "</if>",
            "<if test='alterationBo.proName != null and alterationBo.proName.trim() != \"\"'>",
            "and zb.PROJECT_NAME like CONCAT('%', '${alterationBo.proName}', '%')",
            "</if>",
            "<if test='alterationBo.chargerName != null and alterationBo.chargerName != \"\"'>",
            "and pb.CHARGER_NAME like CONCAT('%', '${alterationBo.chargerName}', '%') ",
            "</if>",
            "<if test='alterationBo.unitId != null and alterationBo.unitId != \"\"'>",
            "and pb.UNIT_ID in ('${alterationBo.unitId}') ",
            "</if>",
            "<if test='alterationBo.proId != null and alterationBo.proId != \"\"'>",
            "and zb.PROJECT_ID in ('${alterationBo.proId}') ",
            "</if>",
            "order by zb.${alterationBo.orderStr} ${alterationBo.orderType}",
            "</script>"
    })
    IPage<BizProjectAlteration> selectListInfos(IPage<BizProjectAlteration> page, @Param(Constants.WRAPPER) Wrapper<BizProjectAlteration> queryWrapper, @Param("alterationBo") AlterationBo alterationBo);

    /**
     * 导出
     * @param queryWrapper
     * @param alterationBo
     * @return
     */
    @Select({
            "<script>",
            "select zb.PROJECT_NAME,pb.CHARGER_NAME,bu.NAME as unitName,dpt.BELONG_TYPE as proTypeName,ALTER_TYPE_NAME,ALTER_REASON," +
            "Date(ALTER_DATE) as alterDate,MANDATARYNAME,Date(MANDATARYBEGINDATE) as mandatarybegindate,Date(MANDATARYENDDATE) as mandataryenddate,zb.CHECKSTATUS as checkstatus,zb.ALTER_TYPE as alterType" +
            " from biz_project_alteration zb ",
            "left join v_project pb on pb.ID = zb.PROJECT_ID ",
            "left join biz_unit bu on pb.unit_id = bu.id ",
            "left join dm_project_type dpt on dpt.CODE = pb.PROJECT_TYPE ",
            "left join (select dict_label,dict_value from sys_dict_data WHERE dict_type = 'bpm_process_instance_status') dc on dc.dict_value = pb.CHECKSTATUS ",
            "<if test='ew.customSqlSegment != null and ew.customSqlSegment != \"\"'>",
            "${ew.customSqlSegment} ",
            "</if>",
            "<if test='ew.customSqlSegment == null or ew.customSqlSegment == \"\"'>",
            "where 1 = 1 ",
            "</if>",
            "<if test='alterationBo.proName != null and alterationBo.proName.trim() != \"\"'>",
            "and pb.name like CONCAT('%', '${alterationBo.proName}', '%') ",
            "</if>",
            "<if test='alterationBo.chargerName != null and alterationBo.chargerName != \"\"'>",
            "and pb.CHARGER_NAME like CONCAT('%', '${alterationBo.chargerName}', '%') ",
            "</if>",
            "<if test='alterationBo.unitId != null and alterationBo.unitId != \"\"'>",
            "and zb.UNIT_ID in ('${alterationBo.unitId}') ",
            "</if>",
            "<if test='alterationBo.proId != null and alterationBo.proId != \"\"'>",
            "and zb.PROJECT_ID in ('${alterationBo.proId}') ",
            "</if>",
            "order by zb.${alterationBo.orderStr} ${alterationBo.orderType}",
            "</script>"
    })
    IPage<BizProjectAlteration> selectListInfosForExport(IPage<BizProjectAlteration> page,@Param(Constants.WRAPPER) Wrapper<BizProjectAlteration> queryWrapper, @Param("alterationBo") AlterationBo alterationBo);

    /**
     * 统计
     * @param queryWrapper
     * @param alterationBo
     * @return
     */
    @Select({
            "<script>",
            "SELECT sdd.dict_label AS className, sdd.dict_value as classValue, COALESCE(COUNT(zbAll.CHECKSTATUS), 0) AS classCount ",
            "FROM ",
            "(SELECT DISTINCT dict_label,dict_value FROM sys_dict_data where dict_type ='bpm_process_instance_status') AS sdd ",
            "LEFT JOIN ",
            "(select zb.* from biz_project_alteration zb left join v_project pb on pb.ID = zb.PROJECT_ID ",
            "<if test='ew.customSqlSegment != null and ew.customSqlSegment != \"\"'>",
            "${ew.customSqlSegment} ",
            "</if>",
            "<if test='ew.customSqlSegment == null or ew.customSqlSegment == \"\"'>",
            "where 1 = 1 ",
            "</if>",
            "<if test='alterationBo.proName != null and alterationBo.proName.trim() != \"\"'>",
            "and PROJECT_NAME like CONCAT('%', '${alterationBo.proName}', '%') ",
            "</if>",
            "<if test='alterationBo.chargerName != null and alterationBo.chargerName != \"\"'>",
            "and zb.CHARGER_NAME like CONCAT('%', '${alterationBo.chargerName}', '%') ",
            "</if>",
            "<if test='alterationBo.unitId != null and alterationBo.unitId != \"\"'>",
            "and zb.UNIT_ID in ('${bizProjectComplete.unitId}') ",
            "</if>",
            "<if test='alterationBo.changeType != null and alterationBo.changeType != \"\"'>",
            "and ALTER_TYPE in ('${alterationBo.changeType}') ",
            "</if>",
            "<if test='alterationBo.proId != null and alterationBo.proId != \"\"'>",
            "and PROJECT_ID in ('${alterationBo.proId}') ",
            "</if>",
            ")zbAll ON zbAll.CHECKSTATUS = sdd.dict_value    ",
            "GROUP BY sdd.dict_label, sdd.dict_value ORDER BY sdd.dict_value  ",
            "</script>"
    })
    List<NavigationForProVo.NameAndCount> selectGroups(@Param(Constants.WRAPPER) Wrapper<NavigationForProVo.NameAndCount> queryWrapper, @Param("alterationBo") AlterationBo alterationBo);

    @Select({
            "<script>",
            "select zb.*,Date(zb.ALTER_DATE) as alterDate,Date(CHANGE_PLAN_END_DATE) as changePlanEndDate, suf.UPLOADFILENAME as filename",
            ",pb.CHARGER_NAME as chargerName,pb.CHARGER_CODE as chargerCode,bu.NAME as unitName,pb.UNIT_ID as unitId ",
            " from biz_project_alteration zb ",
            "left join sys_upload_file suf on zb.ALTER_FILE_ID = suf.ID ",
            "left join v_project pb on pb.ID = zb.PROJECT_ID ",
            "left join biz_unit bu on pb.unit_id = bu.id ",
            "where zb.ID = '${ID}'",
            "</script>"
    })
    BizProjectAlteration selectById(@Param("ID") String ID);
}


